package com.heima.common.exception;

import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 作用：捕获controller层抛出异常，将异常统一进行友好返回
 *
 * @author: itheima
 * @create: 2021-12-22 08:59
 */
@Slf4j
@RestControllerAdvice
public class BaseExceptionHandler {

    /**
     * 捕获到Exception子类异常都会执行该方法
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult handlerException(Exception e) {
        log.error("全局异常捕获：{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
